package com.link4a.doms.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.link4a.doms.Constants;
import com.link4a.doms.service.LicenseService;
import com.link4a.doms.vo.LicenseCondition;
import com.link4a.doms.web.ControllerSupport;
import com.link4a.doms.web.JsonResult;
import com.link4a.doms.web.auth.Privilege;

@Controller
@RequestMapping("license")
public class LicenseController extends ControllerSupport{
	
	@Autowired
	private LicenseService licenseService;
	
	@RequestMapping
	@ResponseBody
	public JsonResult findPage(@RequestBody LicenseCondition condition){
		return success(licenseService.findPage(condition));
	}
	
	@RequestMapping(value="/disable/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult disable(@PathVariable int id){
		licenseService.disable(id);
		return success();
	}
	
	@RequestMapping(value="/enable/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult enable(@PathVariable int id){
		licenseService.enable(id);
		return success();
	}
	
	@RequestMapping
	@Privilege(Constants.LICENSE_INDEX)
	public ModelAndView index(){
		ModelAndView mav = new ModelAndView("/business/license");
		return mav;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value="/check/{mac}/{license}")
	@ResponseBody
	public boolean check(@PathVariable String mac, @PathVariable String license){
		try {
			return licenseService.onCheck(mac, license);
		} catch (Throwable e) {
			return false;
		}
	}
}
